package vashion.azeroth.api.controller.member;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.personal.pojo.PersonalExperience;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalExperienceService;

@Controller
@RequestMapping("/member")
public class ExperienceController<T> extends BaseController {

	private final static Logger log = LoggerFactory.getLogger(ExperienceController.class);
	
	@Autowired
	private PersonalExperienceService<T> personalExperienceService;

	/**
	 * 添加经验值
	 * 
	 * */
//	@RequestMapping("addExperience.htm")
//	@ResponseBody
//	public ApiResult<PersonalExperience> addExperience(
//			@ModelAttribute("PersonalExperience") PersonalExperience experience) {
//		ApiResult<PersonalExperience> result = personalFavoritesService
//				.addExperience(experience);
//		return result;
//	}

	/**
	 * 获取实际经验值
	 * 
	 * */
	@RequestMapping("getExperience.htm")
	@ResponseBody
	public ApiResult<Integer> getExperience(
			@ModelAttribute("PersonalExperience") PersonalExperience experience) {

		ApiResult<Integer> result = new ApiResult<Integer>();
		try {
			if (null == experience) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == experience.getMemberId()
					|| StringUtils.isBlank(String.valueOf(experience
							.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员id"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}
			result = personalExperienceService.getExperienceByMemberId(experience);
		} catch (Exception e) {
			log.error("获取实际经验值", e);
		}
		return result;
	}
	
	/**
	 * 计算上、下线对当前用户造成的经验值影响，更新实际经验值（value字段）
	 * 
	 * */
	@RequestMapping("updateExperience.htm")
	@ResponseBody
	public ApiResult<Double> updateExperience(
			@ModelAttribute("PersonalExperience") PersonalExperience experience) {

		ApiResult<Double> result = new ApiResult<Double>();
		try {
			if (null == experience) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == experience.getMemberId()
					|| StringUtils.isBlank(String.valueOf(experience
							.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员id"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}
			result = personalExperienceService.updateExperienceMemberId(experience);
		} catch (Exception e) {
			
		}
		return result;
	}
	
	
	/**
	 * 计算用户行为对当前用户base经验值造成影响，更新base经验值（value_base字段）
	 * 
	 * */
	@RequestMapping("updateExperienceBase.htm")
	@ResponseBody
	public ApiResult<Integer> updateExperienceBase(
			@ModelAttribute("PersonalExperience") PersonalExperience experience) {

		ApiResult<Integer> result = new ApiResult<Integer>();
		try {
			if (null == experience) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == experience.getMemberId()
					|| StringUtils.isBlank(String.valueOf(experience
							.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员id"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}
			result = personalExperienceService.updateExperienceBase(experience);
		} catch (Exception e) {
			
		}
		return result;
	}

}
